var isPlayOnWeb = false;

// 查找视频标签并处理
function findAndProcessVideoTags() {
    var videoTags = document.getElementsByTagName("video");
    var videoSrcs = [];
    for (var index = 0; index < videoTags.length; index++) {
        var videoTag = videoTags[index];
        if (videoTag.getAttribute("marked") === "1") {
            continue;
        }
        // 获取视频的 src 属性
        var videoSrc = videoTag.src;
        if (!videoSrc) {
            var sources = videoTag.getElementsByTagName("source");
            if (sources.length > 0) {
                videoSrc = sources[0].src; // 使用第一个 <source> 的 src
                videoSrcs.push(videoSrc);
            }
        }
        videoTag.addEventListener("webkitbeginfullscreen", onEnterFullScreenEvent, true);
        videoTag.addEventListener("webkitendfullscreen", onExitFullScreenEvent, true);
        videoTag.addEventListener("play", onVideoPlayEvent, true);
        videoTag.addEventListener("pause", onVideoPauseEvent, true);
        videoTag.setAttribute("position", index);
        videoTag.setAttribute("marked", "1");
    }
    if (window.android != null && typeof (window.android) !== "undefined") {
        if (videoSrcs.length > 0) {
            window.android.detectedVideo(JSON.stringify(videoSrcs));
        }
    }
}

function onEnterFullScreenEvent() {
    // 处理全屏进入事件
}

function onExitFullScreenEvent() {
    // 处理全屏退出事件
}

function onVideoPlayEvent() {
    if (!isPlayOnWeb) {
        checkIsNeedUsePlayerOfApp(this);
    }
}

function onVideoPauseEvent() {
    // 处理视频暂停事件
}

function checkIsNeedUsePlayerOfApp(videoTag) {
    if (shouldUseNativePlayer()) {
        var i = setInterval(function () {
            notifyAppPlayerToPlay(videoTag);
//            videoTag.pause();
            clearInterval(i);
        }, 200);
    }
}

function shouldUseNativePlayer() {
    // 这里可以根据具体需求添加判断逻辑，例如判断视频类型等
    return true;
}

/**
 * notify app to play video
 * @param videoTag
 */
function notifyAppPlayerToPlay(videoTag) {
    var videoSrc = videoTag.currentSrc;
    var sourceArr = [];
    var sources = videoTag.getElementsByTagName("source");
    if (sources && sources.length > 0) {
        for (var index = 0; index < sources.length; index++) {
            var sourceInfo = {
                'url': sources[index].getAttribute("src"),
                'media': sources[index].getAttribute("media"),
                'type': sources[index].getAttribute("type")
            };
            sourceArr.push(sourceInfo);
        }
    }
    var videoInfo = {
        'videoUrl': videoSrc,
        'source': sourceArr,
        'title': document.title,
        'url': document.URL,
        'position': videoTag.getAttribute('position')
    };
    if (window.android != null && typeof (window.android) !== "undefined") {
        window.android.chickVideo(JSON.stringify(videoInfo));
    }
}

function setPlayOnWeb() {
    isPlayOnWeb = true;
}

// 使用 MutationObserver 监听 DOM 变化
var observer = new MutationObserver(findAndProcessVideoTags);
observer.observe(document.body, { childList: true, subtree: true });

// 初始查找
findAndProcessVideoTags();